<%@ page contentType="text/javascript;charset=UTF-8" language="java" %><%@ page
import="org.apache.commons.io.FileUtils"%><%@ page
import="org.apache.commons.lang3.ObjectUtils"%><%@ page
import="org.codehaus.jackson.map.ObjectMapper"%><%@ page
import="org.codehaus.jackson.type.TypeReference"%><%@ page
import="org.yaml.snakeyaml.Yaml"%><%@ page
import="java.io.File"%><%@ page
import="java.io.FileInputStream"%><%@ page
import="java.util.HashMap"%><%@ page
import="java.util.Map"%><%@ page
import="java.util.Set"%><%@ page
import="java.util.TreeMap"%><%
        final ObjectMapper objectMapper = new ObjectMapper();
        final String hostYmlName = "hosts.yml";
        final String confPath = "/0/conf/";
        final Map<String,Object> cache = new TreeMap<String, Object>();
        Object hostsYml = new Yaml().load(new FileInputStream(new File(application.getRealPath(confPath+hostYmlName))));
        cache.put(hostYmlName, hostsYml);
        HashMap<String, HashMap<String, String>> conf = objectMapper.convertValue(hostsYml,
                new TypeReference<HashMap<String, HashMap<String, String>>>() { });
        HashMap<String,String> hosts = ObjectUtils.defaultIfNull(conf.get(request.getServerName()), new HashMap<String, String>());

        Set<String> resourcePaths = application.getResourcePaths(confPath);
        for (String resourcePath : resourcePaths) {
            if(resourcePath.endsWith(".yml") && !resourcePath.endsWith("/conf/hosts.yml")){
                File file = new File(application.getRealPath(resourcePath));
                String fileContent = FileUtils.readFileToString(file,"utf8");
                for(Map.Entry<String,String> entry:hosts.entrySet()){
                    fileContent = fileContent.replaceAll("(http?://)"+entry.getKey()+"/", "$1"+entry.getValue()+"/");
                }
                cache.put(file.getName(), new Yaml().load(fileContent));
            }
        }
        String json = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(cache);
        %>angular.element.data(angular, 'panel-conf-cache', <%=json%>);